%Macro Manip;
Data one; set one;
retain insectno2;
if insectno2<1 then insectno2=0;
insectno2=insectno2+1;
Data two three; set one;
if mod(insectno2,2) eq 1 then output two;
if mod(insectno2,2) eq 0 then output three;
Data two; set two; drop Var1 Var2 Var3 Var4 Var5 Var6 Var7 Var8 Var9 insectno2;
Data two; set two;
	retain insectno2;
	if insectno2<1 then insectno2=0;
	insectno2=insectno2+1;
data three; set three;
var9=var8; var8=var7; var7=var6; var6=var5; var5=var4; var4=var3; var3=var2; var2=var1; var1=var1; /*Use "waveform;" in place of "var1;" to switch to TBF values.*/;
data three; set three; drop waveform insectno2;
data three; set three;
  retain insectno2;
  if insectno2<1 then insectno2=0;
  insectno2=insectno2+1;
data two; set two three; merge two three; by insectno2;
data two; set two; drop Var2 Var3 Var4 Var5 Var6 Var7 Var8 Var9;
%mend;

options ls=100 ps=72;

Data one;
      infile 'C:\Users\tebert\Desktop\EPG Web Page Folder\Worked Examples\Psyllid Data\Control 1' dsd missover;
      input waveform$ Var1 Var2 Var3 Var4 Var5 Var6 Var7 Var8 Var9;
Data one; Set one;
%Manip;
data two; set two; insectno="a01";
data four; set two;
Run;
/*
/******* This is the generic unit of this program.      ***********************
*****  For every individual there will be a separate file. *************************
*****  For every file there will be the following 8 lines of code. ******************
***** Copy and paste this section, changing filename and increment insectno by 1 ***
************************************************************************************/
Data one; infile 'C:\Users\tebert\Desktop\EPG Web Page Folder\Worked Examples\Psyllid Data\Control 2' dsd missover; input waveform$ Var1 Var2 Var3 Var4 Var5 Var6 Var7 Var8 Var9; Data one; Set one; %Manip; data two; set two; insectno="a02"; proc append base=four data=two; Run;
Data one; infile 'C:\Users\tebert\Desktop\EPG Web Page Folder\Worked Examples\Psyllid Data\Control 6' dsd missover; input waveform$ Var1 Var2 Var3 Var4 Var5 Var6 Var7 Var8 Var9; Data one; Set one; %Manip; data two; set two; insectno="a06"; proc append base=four data=two; Run;
Data one; infile 'C:\Users\tebert\Desktop\EPG Web Page Folder\Worked Examples\Psyllid Data\Control 7' dsd missover; input waveform$ Var1 Var2 Var3 Var4 Var5 Var6 Var7 Var8 Var9; Data one; Set one; %Manip; data two; set two; insectno="a07"; proc append base=four data=two; Run;
Data one; infile 'C:\Users\tebert\Desktop\EPG Web Page Folder\Worked Examples\Psyllid Data\Control 8' dsd missover; input waveform$ Var1 Var2 Var3 Var4 Var5 Var6 Var7 Var8 Var9; Data one; Set one; %Manip; data two; set two; insectno="a08"; proc append base=four data=two; Run;

Data one; infile 'C:\Users\tebert\Desktop\EPG Web Page Folder\Worked Examples\Psyllid Data\Water 3' dsd missover; input waveform$ Var1 Var2 Var3 Var4 Var5 Var6 Var7 Var8 Var9; Data one; Set one; %Manip; data two; set two; insectno="b03"; proc append base=four data=two; Run;
Data one; infile 'C:\Users\tebert\Desktop\EPG Web Page Folder\Worked Examples\Psyllid Data\Water 4' dsd missover; input waveform$ Var1 Var2 Var3 Var4 Var5 Var6 Var7 Var8 Var9; Data one; Set one; %Manip; data two; set two; insectno="b04"; proc append base=four data=two; Run;
Data one; infile 'C:\Users\tebert\Desktop\EPG Web Page Folder\Worked Examples\Psyllid Data\Water 5' dsd missover; input waveform$ Var1 Var2 Var3 Var4 Var5 Var6 Var7 Var8 Var9; Data one; Set one; %Manip; data two; set two; insectno="b05"; proc append base=four data=two; Run;
Data one; infile 'C:\Users\tebert\Desktop\EPG Web Page Folder\Worked Examples\Psyllid Data\Water 6' dsd missover; input waveform$ Var1 Var2 Var3 Var4 Var5 Var6 Var7 Var8 Var9; Data one; Set one; %Manip; data two; set two; insectno="b06"; proc append base=four data=two; Run;
Data one; infile 'C:\Users\tebert\Desktop\EPG Web Page Folder\Worked Examples\Psyllid Data\Water 7' dsd missover; input waveform$ Var1 Var2 Var3 Var4 Var5 Var6 Var7 Var8 Var9; Data one; Set one; %Manip; data two; set two; insectno="b07"; proc append base=four data=two; Run;

**************************  End fundamental unit ************************************
**************** Paste one copy for each individual and make changes ****************
*************************************************************************************



************** Do not copy or paste anything below this line ************************
************************************ But ********************************************
************* Please change the file name in the Proc Export line *******************
******************  before running the program. Any old copy of  ********************
******************  this file will be replaced due to the "replace" statement *******
******************  Var1 is from start of recording.              *******************
******************  Var2 is from when insect is on the plant.     *******************
*************************************************************************************/;
data four; set four; waveform=compress(waveform);
Data four; set four;
	retain w0 w1 in0 marker1;
	w1=Compress(upcase(waveform));
	if insectno ne in0 then do;
	  w0='  ';
	  in0=insectno; marker1=0;
	end;
	Dur=var1-marker1;
	marker1=var1;
data four; set four; drop var1 w0 w1 in0 marker1;
proc sort; by insectno;
data five; set four; drop waveform dur insectno2;
proc sort; by insectno;
data five; set five four; merge five four; by insectno;
data five; set five; drop insectno2;
proc export data=five outfile='C:\Users\tebert\Desktop\EPG Web Page Folder\Worked Examples\Psyllid Data\ExampleW.csv' dbms=csv replace;
proc print data=five;
Run;
Quit;
